﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Car_Rental.Models;
using Data.Car_Rental;
using Entity.Car_Rental;
using Web.Attributes;
using Web.Commons;
using Entity.CommonEnums;
using Car_Rental.Controllers.Helpers;
using Standard;
namespace Car_Rental.Controllers
{

    [HandleError]
    public class AccountController : BaseController
    {

        IUserRepository iUserRepository = null;
        public AccountController()
        {
            iUserRepository = new UserRepository();
        }


        #region 登陆和登出
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(UserBases entity)
        {
            CommonMethod.UserLogOnModule(this, entity);
            return View(entity);
        }


        public ActionResult LogOff()
        {
            #region 清Cookies
            CommonMethod.UserLogOutModule(this);
            #endregion

            return RedirectToAction("Index", "Home");
        }
        #endregion

        #region 用户注册
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(UserBases entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DateTime.Now;
            entity.Status = (int)Status.Normal;
            entity.Password = entity.Password;
            entity.ConfirmPassword = entity.ConfirmPassword;
            ModelState.Clear();//清除默认的验证
            if (entity.IsValid)
            {

                if (iUserRepository.RegisterUser(entity).IsComplete)
                {
                    #region 模拟登陆，写cookies
                    entity.Password = entity.ConfirmPassword;//这时entity.Password是md5后的了，所以要重新赋一下值
                    CommonMethod.UserLogOnModule(this, entity);
                    #endregion
                    return RedirectToAction("RegSuccess", "Account");
                }
                else
                {
                    ModelState.AddModelError("", "注册用户失败");
                }
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View();
        }
        public ActionResult RegSuccess()
        {
            return View();
        }

        #endregion

        #region 修改密码
        [UserAuthentication]
        public ActionResult ChangePassword()
        {
            return View();
        }

        [UserAuthentication]
        [HttpPost]
        public ActionResult ChangePassword(FormCollection form)
        {
            UserBases entity = new UserBases();
            TryUpdateModel(entity);
            entity.ValidFields = new string[] { "Password", "NewPassword", "ConfirmPassword" };
            if (entity.IsValid)
            {
                if (iUserRepository.ChangePassword(entity).IsComplete)
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "当前密码不正确或新密码无效");
                }
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View();
        }
        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }
        #endregion



    }


}
